package ru.isokolov.lib;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

class ExceptionDict {
    public static final ExceptionDict exceptionDic = new ExceptionDict("Except.dic");
    private final String fileName;
    private volatile Map<String, List<String>> exceptionMap;

    ExceptionDict(String fileName) {
        this.fileName = fileName;
        this.readFile();
    }

    public void readExceptionStrings(Iterator<String> it) {
        Map<String, List<String>> exceptions = new HashMap<>();
        ArrayList strings = null;

        while (true) {
            while (true) {
                String row;
                do {
                    do {
                        do {
                            if (!it.hasNext()) {
                                if (strings != null) {
                                    Collections.sort(strings);
                                }

                                this.exceptionMap = exceptions;
                                return;
                            }

                            row = it.next();
                        } while (row == null);

                        row = row.trim();
                    } while (row.isEmpty());
                } while (row.charAt(0) == ';');

                String sectionName;
                if (row.charAt(0) == '[' && row.charAt(row.length() - 1) == ']') {
                    if (strings != null) {
                        Collections.sort(strings);
                        strings = null;
                    }

                    sectionName = row.substring(1, row.length() - 1);
                    if (!sectionName.isEmpty()) {
                        strings = new ArrayList();
                        exceptions.put(sectionName, strings);
                    }
                } else if (strings != null) {
                    sectionName = row.toLowerCase();
                    sectionName = StrUtils.extractWord(1, sectionName, "\t;");
                    sectionName = sectionName.replaceAll(" ", "");
                    strings.add(sectionName);
                }
            }
        }
    }

    private BufferedReader openFile() throws UnsupportedEncodingException {
        BufferedReader in;

        try {
            in = new BufferedReader(new InputStreamReader(new FileInputStream(this.fileName), "Cp1251"));
        } catch (Exception var6) {
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            InputStream pkgDic = loader.getResourceAsStream(this.fileName);
            if (pkgDic == null) {
                String pkg = this.getClass().getPackage().getName();
                pkgDic = loader.getResourceAsStream(pkg.replace('.', '/') + "/" + this.fileName);
            }

            in = new BufferedReader(new InputStreamReader(pkgDic, "Cp1251"));
        }

        return in;
    }

    private void readFile() {
        if (!this.fileName.isEmpty()) {
            BufferedReader in = null;

            try {
                in = this.openFile();
                BufferedReader finalIn = in;
                Iterator<String> it = new Iterator<String>() {
                    public boolean hasNext() {
                        try {
                            return finalIn.ready();
                        } catch (IOException var2) {
                            System.out.println(var2);
                            return false;
                        }
                    }

                    public String next() {
                        try {
                            return finalIn.readLine();
                        } catch (IOException var2) {
                            System.out.println(var2);
                            return null;
                        }
                    }

                    public void remove() {
                    }
                };
                this.readExceptionStrings(it);
            } catch (Exception var13) {
                System.out.println("Error opening " + var13.getMessage());
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException var12) {
                }
            }
        }

    }

    private List<String> getListFor(String section) {
        Map<String, List<String>> exceptions = this.exceptionMap;
        return exceptions == null ? null : exceptions.get(section);
    }

    public boolean present(String anyWord, String section) {
        boolean result = false;
        if (this.fileName.isEmpty()) {
            return result;
        } else {
            List<String> l = this.getListFor(section);
            if (l == null) {
                return result;
            } else {
                anyWord = anyWord.toLowerCase();
                int i = Collections.binarySearch(l, anyWord);
                result = i >= 0;
                if (result) {
                    return result;
                } else if ((section.equalsIgnoreCase("LastName") || section.equalsIgnoreCase("LastNameW") || section.equalsIgnoreCase("DependedLastNameW") || section.equalsIgnoreCase("FirstNameM") || section.equalsIgnoreCase("FirstNameW") || section.equalsIgnoreCase("FirstPartLastName") || section.equalsIgnoreCase("Accent")) && l.size() > 0) {
                    List<String> edMask = new ArrayList();
                    i = 0;
                    String s = (String) l.get(i);

                    while (s.charAt(0) == '*') {
                        edMask.add(s.substring(1));
                        ++i;
                        if (i < l.size()) {
                            s = (String) l.get(i);
                        } else {
                            s = " ";
                        }
                    }

                    for (i = 0; i < edMask.size(); ++i) {
                        String mask = edMask.get(i);
                        int start = anyWord.length() - mask.length();
                        if (start < 0) {
                            start = 0;
                        }

                        s = anyWord.substring(start);
                        if (s.equals(mask) || mask.equals("*")) {
                            result = true;
                            return result;
                        }
                    }

                    return result;
                } else {
                    return result;
                }
            }
        }
    }

    public int accentInfo(String anyWord) {
        int[] arrVocalic = new int[20];
        int result = 0;
        anyWord = anyWord.toLowerCase();
        int j = 0;

        int i;
        for (i = 0; i < anyWord.length(); ++i) {
            if ("аоуыэяеёюи".indexOf(anyWord.charAt(i)) >= 0) {
                arrVocalic[j] = i;
                ++j;
            }
        }

        --j;

        for (i = 0; i <= j; ++i) {
            StringBuilder b = new StringBuilder();
            String tmp = b.append(anyWord.substring(0, arrVocalic[i])).append('"').append(anyWord.substring(arrVocalic[i])).toString();
            if (this.present(tmp, "Accent")) {
                if (i == j) {
                    result = 1;
                } else {
                    result = 2;
                }
            }
        }

        return result;
    }

    public String getRightPart(String leftPart, String section) {
        leftPart = leftPart.toLowerCase();
        List<String> l = this.getListFor(section);
        if (l != null) {
            if (!l.isEmpty()) {
                for (String string : l) {
                    int p = string.indexOf(61);
                    if (leftPart.equals(string.substring(0, p).trim())) {
                        return string.substring(p + 1).trim();
                    }
                }
            }

        }
        return leftPart;
    }
}
